<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Requests\UserInsertRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Hash;
use DB;

class LoginController extends Controller
{
	/*
		显示登录页面
	*/
	public function getIndex()
	{
		//解析模板
		return view('admin.login.login');
	}

	/*
		执行登录
	*/
	public function postInsert(Request $request)
	{
		//判断用户名是否存在
		$name = $request->input('uname');
		$user = DB::table('shop_users')->where('uname',$name)->first();
		if(empty($user)){
			return back()->with('error','用户名或密码不正确');
		}
		//判断权限
		$auth = $user->auth;
		if($auth == 0){
			return back()->with('error','未获取权限');
		}
		//密码验证
		$res = Hash::check($request->input('upwd'),$user->upwd);
		if ($res) {
			//将用户信息写入session
			session(['uid'=>$user->uid,'uname'=>$user->uname,'uface'=>$user->uface]);
			return redirect('/admin/index')->with('success','欢迎'.$name.'登陆');
		}else{
			return back()->with('error','用户名或密码不正确');
		}
	}

	/*
		用户退出
	*/
	public function getExit(Request $request)
	{
		$request->session()->flush();
		return redirect('/admin/login');
	}
}